import time
from selenium.webdriver.common.by import By
import allure


class Tools:
    def __init__(self, driver):
        # driver：浏览器，只在传递进来的浏览器操作
        self.driver = driver
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def visit(self, url):
        # url:网页地址
        self.driver.get(url)
        time.sleep(1)

    # 根据css定位
    def by_css(self, css):
        return self.driver.find_element(By.CSS_SELECTOR, css)

    # 根据link_text定位
    def by_link(self, link):
        return self.driver.find_element(By.PARTIAL_LINK_TEXT, link)

    # 关闭浏览器
    def quit(self):
        self.driver.quit()

    # 截图
    def img(self):
        title = time.strftime('%Y_%m_%d_%H_%M_%S')
        # 截图、附加在测试报告中
        allure.attach(self.driver.get_screenshot_as_png(), title, allure.attachment_type.PNG)
